import click
import os

import core
import package
import data
import config_
import script

@click.group(chain=True)
@click.pass_context
def cli(ctx):
    ctx.ensure_object(dict)

    conf = config_.get_config()
    conf.load()

    ctx.obj['config'] = conf

@cli.command()
@click.option("--git",type=str,help="The git executable's location.")
@click.option("--python",type=str,help="The python executable's location.")
@click.option("--mirror",type=str,help="The mirror for pip.")
@click.pass_context
def config(ctx, git, python, mirror):
    conf = ctx.obj["config"]
    if git and type(git) == str:
        conf.set("git", git)
    if python and type(python) == str:
        conf.set("python",python)
    if mirror and type(mirror) == str:
        conf.set("mirror", mirror)
    conf.save()

@cli.command()
@click.option("-r","--repos",type=str,help="The repository of the package.")
@click.pass_context
def download(ctx,repos):
    if not repos: 
        click.echo("Argument -r/--repos is required.")
        return
    conf = ctx.obj["config"]
    try:
        name = package.get_package_meta(repos)["name"]
    except:
        click.echo("Fail to fetch metafile of the package.")
    
    packages_dir = data.get_packages_path()
    package_dir = os.path.join(packages_dir,name)

    success = package.download_package_from_repository(conf.get("git"), repos, package_dir)

    if success:
        click.echo("Download success")
    else:
        click.echo("Download fail")

@cli.command()
@click.option("-r","--repos",type=str,help="The repository of the static package.")
@click.pass_context
def download_static(ctx,repos):
    if not repos: 
        click.echo("Argument -r/--repos is required.")
        return
    conf = ctx.obj["config"]
    try:
        name = package.get_package_meta(repos,static=True)["name"]
    except:
        click.echo("Fail to fetch metafile of the package.")

    packages_dir = data.get_packages_path()
    package_dir = os.path.join(packages_dir,name)

    package.download_package_from_repository(conf.get("git"), repos, package_dir)
    success = package.download_package_from_repository(conf.get("git"), repos, package_dir)

    if success:
        click.echo("Download success")
    else:
        click.echo("Download fail")

@cli.command()
@click.option("-n","--name",type=str,help="The name of the package.")
@click.pass_context
def venv(ctx, name):
    if not name:
        click.echo("Argument -n/--name is required.")
        return
    conf = ctx.obj["config"]
    packages_dir = data.get_packages_path()
    package_dir = os.path.join(packages_dir,name)

    environments_dir = data.get_environments_path()
    environment_dir = os.path.join(environments_dir,name)

    package.create_environment(environment_dir,package_dir,conf.get("python"),conf.get("mirror"))

@cli.command()
@click.option("--app",type=str,help="The fuse app folder.")
def run(app):
    if not app:
        click.echo("Argument --app is required.")
        return
    try:
        target = script.get_app_data(os.path.realpath(app))
        script.Runtime(target).run()
    except core.AppError as e:
        print(e.id)

if __name__ == "__main__":
    try:
        cli(obj={})
    except core.AppError as e:
        print(e.id)